import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class RefundService extends BaseService<any> {
  private readonly logger = new Logger(RefundService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getPage
   * 对应 PHP: RefundService_admin::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(where: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: $field = 'id,refund_no,out_trade_no,type,channel,money,reason,status,create_time,refund_time,close_time,fail_reason,voucher,trade_type,trade_id,refund...
const field = 'id,refund_no,out_trade_no,type,channel,money,reason,status,create_time,refund_time,close_time,fail_reason,voucher,trade_type,trade_id,refund_type,main_type,main_id';
        const search_model = this.model.where([ [ 'site_id', '=', this.site_id ) ))
            .withSearch([ 'create_time', 'out_trade_no', 'refund_no', 'status' ), where)
            .field(field)
            .append([ 'type_name', 'status_name','trade_type_name' ))
            .order('create_time desc'];
        return this.pageQuery(search_model);
    }

    /**
     * 获取退款详情
     * @param string refund_no
     * @return */
    async getDetail(string refund_no){
        const field = 'id,refund_no,out_trade_no,type,channel,money,reason,status,create_time,refund_time,close_time,fail_reason,voucher,trade_type,trade_id,refund_type,main_type,main_id';
        return this.model.where([ ['refund_no', '=', refund_no ], [ 'site_id', '=', this.site_id ) ))
            .field(field)
            .append([ 'type_name', 'status_name', 'refund_type_name' ))
            .findOrEmpty()
            .toArray(];
    }

    /**
     * 支付审核通过
     * @param data
     * @return bool
     */
    async refund(data) {
        return this.coreRefundService.refund(this.site_id, data.refund_no, data.voucher, data.refund_type, PayDict.USER, this.uid);
    }

}
  }

  /**
   * getDetail
   * 对应 PHP: RefundService_admin::getDetail()
   * 逻辑类型: undefined - undefined
   */
  async getDetail(refund_no: string) {
    // 基于PHP真实逻辑: getDetail
    // PHP原文: $field = 'id,refund_no,out_trade_no,type,channel,money,reason,status,create_time,refund_time,close_time,fail_reason,voucher,trade_type,trade_id,refund...
const field = 'id,refund_no,out_trade_no,type,channel,money,reason,status,create_time,refund_time,close_time,fail_reason,voucher,trade_type,trade_id,refund_type,main_type,main_id';
        return this.model.where([ ['refund_no', '=', refund_no ], [ 'site_id', '=', this.site_id ) ))
            .field(field)
            .append([ 'type_name', 'status_name', 'refund_type_name' ))
            .findOrEmpty()
            .toArray(];
    }

    /**
     * 支付审核通过
     * @param data
     * @return bool
     */
    async refund(data) {
        return this.coreRefundService.refund(this.site_id, data.refund_no, data.voucher, data.refund_type, PayDict.USER, this.uid);
    }

}
  }

  /**
   * refund
   * 对应 PHP: RefundService_admin::refund()
   * 逻辑类型: undefined - undefined
   */
  async refund(data: any[]) {
    // 基于PHP真实逻辑: refund
    // PHP原文: return (new CoreRefundService())->refund($this->site_id, $data['refund_no'], $data['voucher'], $data['refund_type'], PayDict::USER, $this->uid);     }...
return this.coreRefundService.refund(this.site_id, data.refund_no, data.voucher, data.refund_type, PayDict.USER, this.uid);
    }

}
  }
}
